package com.xyc.servlet.main;

import com.xyc.mapper.Admin_userMapper;
import com.xyc.mapper.UserMapper;
import com.xyc.mapper.impl.UserMapperImpl;
import com.xyc.pojo.Admin_user;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");

        HttpSession session = req.getSession();
        session.setAttribute("username", username);
        String password = req.getParameter("password");

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        Admin_userMapper mapper = ctx.getBean("admin_userMapper", Admin_userMapper.class);
        Admin_user userList = mapper.selectAdmin_user(username);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");

        if (userList.getPassword().equals(password)) {
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        } else { // 密码不对，返回登录界面
            req.getRequestDispatcher("login.html").forward(req, resp);
        }
    }
}
